package com.lwei.ljpro.controller;

import com.lwei.ljpro.dto.Message;
import com.lwei.ljpro.emun.CookieName;
import com.lwei.ljpro.emun.ProcessResult;
import com.lwei.ljpro.entry.UserEntry;
import com.lwei.ljpro.entry.WorkingEntry;
import com.lwei.ljpro.service.IWorkingService;
import com.lwei.ljpro.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/customerRest")
public class CustomerRestController {

    private Logger logger = LoggerFactory.getLogger("customerRest");
    @Autowired
    IWorkingService workingService;

    @RequestMapping("/list")
    public Message<WorkingEntry> listCustomerWork(HttpSession session){

        UserEntry currentUser = (UserEntry) session.getAttribute(CookieName.USER_CURRENT.getValue());
        List<WorkingEntry> workingEntryList = workingService.listWorkingByUser(currentUser);
        return new Message<>(ProcessResult.SUCCESS, "用户工作计划列表", workingEntryList);
    }

    @RequestMapping(value = "/list-today", method = RequestMethod.GET)
    public Message<WorkingEntry> listTodayWork(HttpSession session){

        UserEntry currentUser = (UserEntry) session.getAttribute(CookieName.USER_CURRENT.getValue());
        List<WorkingEntry> workingEntryList = workingService.listWorkingByUserToday(currentUser);
        return new Message<>(ProcessResult.SUCCESS, "当日工作计划列表", workingEntryList);
    }

    @RequestMapping(value = "/list-tomorrow",method = RequestMethod.GET)
    public Message<WorkingEntry> listTomorrowWork(HttpSession session){

        UserEntry currentUser = (UserEntry) session.getAttribute(CookieName.USER_CURRENT.getValue());
        List<WorkingEntry> workingEntryList = workingService.listWorkingByUserTomorrow(currentUser);
        return new Message<>(ProcessResult.SUCCESS, "明日工作计划列表", workingEntryList);
    }

    /**
     * 创建新计划
     * @param workingEntry 表单对象
     * @return 结果
     */
    @RequestMapping("/save")
    public Message saveCustomerWork(WorkingEntry workingEntry, HttpSession session, @Param("time") String time){
        UserEntry currentUser = (UserEntry) session.getAttribute(CookieName.USER_CURRENT.getValue());

        switch (time){
            case "today" :
                workingEntry.setPlanTime(DateUtil.today());
                workingEntry.setDoneTime(DateUtil.todayEnd());
                workingEntry.setDoneTimeEnd(DateUtil.todayEnd());
                break;
            case "tomorrow" :
                workingEntry.setPlanTime(DateUtil.tomorrow());
                break;

            default:break;
        }
        Boolean result = workingService.saveWorkingEntry(workingEntry, currentUser);

        if(result){
            return Message.instance(ProcessResult.SUCCESS);
        } else{
            return Message.instance(ProcessResult.FAIL);
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Message edit(WorkingEntry workingEntry){

        WorkingEntry entry = workingService.updateWorkingEntry(workingEntry);

        if(entry != null){
            return Message.instance(ProcessResult.SUCCESS);
        } else{
            return Message.instance(ProcessResult.FAIL);
        }
    }
}
